//
//  Store_View.m
//  HomePage
//
//  Created by winston on 13-4-10.
//  Copyright (c) 2013年 winston. All rights reserved.
//

#import "Store_View.h"
#import "StoreItemView.h"
#import "../../ResourceLoader/ResourceLoader/JSONKit.h"

#define SpaceLeft       30
#define SpaceTop        45
#define LeftDistance    70
#define TopDistance     170

@implementation Store_View

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {

        UILabel * titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(160, 25, 200, 35)];
        titleLabel.backgroundColor = [UIColor clearColor];
        titleLabel.textAlignment = NSTextAlignmentLeft;
        titleLabel.textColor = [UIColor colorWithRed:0.4 green:0.4 blue:0.4 alpha:1.0];
        titleLabel.font = [UIFont fontWithName:@"Arial" size:30];
        [self addSubview:titleLabel];
        [titleLabel release];
        [titleLabel setText:@"本店优势"];
        
        UIImageView * lineView = [[UIImageView alloc] initWithFrame:CGRectMake((frame.size.width-658)/2,70, 658, 8)];
        lineView.image = [UIImage imageNamed:@"AllAuto_Config_TimeLine_BG"];
        [self addSubview:lineView];
        [lineView release];
        
//        [self ReloadView];
        
        
    }
    return self;
}


-(void)ReloadView
{
    int row = 0;
    int width = 250;
    int height = 212;
    
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString * localpath= [NSString stringWithFormat:@"%@/paddata",[paths objectAtIndex:0]];
    NSString * localfilepath = [NSString stringWithFormat:@"%@/local.txt",localpath];
    NSData * data = [NSData dataWithContentsOfFile:localfilepath];
    NSDictionary * templateDic = [data objectFromJSONData];
    
    if (templateDic == nil) {
        return;
    }
    
    for (int i=0; i<6; i++) {
        
        row = i/3;
        
        StoreItemView * itemview = [[StoreItemView alloc] initWithFrame:CGRectMake(LeftDistance + (width+SpaceLeft)*(i%3), TopDistance + (height+SpaceTop)*row, width, height)];
        itemview.tag = 100+i;
        itemview.delegate = self;
        [self addSubview:itemview];
        [itemview release];
        
        NSMutableDictionary * dic = [NSMutableDictionary dictionary];
        NSDictionary * subdic = nil;
        NSString * title = nil;
        NSString * imagepath = nil;
        NSArray * array = nil;
        
        switch (i) {
            case 0:
                subdic = [templateDic objectForKey:@"summary"];
                title = @"本店简介";
                [dic setObject:title forKey:@"StoreTitle"];
                [dic setObject:subdic==nil?[NSDictionary dictionary]:subdic forKey:@"StoreData"];
                array = (NSArray *)[subdic objectForKey:@"pic"];
                if (array.count >0) {
                    imagepath = [NSString stringWithFormat:@"%@/local/%@",localpath,[array objectAtIndex:0]];
                }
                
                itemview.dataSource = dic;
                break;
            case 1:

                subdic = [templateDic objectForKey:@"honor"];
                title = @"本店荣誉";
                [dic setObject:title forKey:@"StoreTitle"];
                [dic setObject:subdic==nil?[NSDictionary dictionary]:subdic forKey:@"StoreData"];
                array = (NSArray *)[subdic objectForKey:@"pic"];
                if (array.count >0) {
                    imagepath = [NSString stringWithFormat:@"%@/local/%@",localpath,[array objectAtIndex:0]];
                }
                
                itemview.dataSource = dic;
                break;
            case 2:

                subdic = [templateDic objectForKey:@"history"];
                title = @"发展历程";
                [dic setObject:title forKey:@"StoreTitle"];
                [dic setObject:subdic==nil?[NSDictionary dictionary]:subdic forKey:@"StoreData"];
                array = (NSArray *)[subdic objectForKey:@"pic"];
                if (array.count >0) {
                    imagepath = [NSString stringWithFormat:@"%@/local/%@",localpath,[array objectAtIndex:0]];
                }
                
                itemview.dataSource = dic;
                break;
            case 3:
                subdic = [templateDic objectForKey:@"strength"];
                title = @"团队实力";
                [dic setObject:title forKey:@"StoreTitle"];
                [dic setObject:subdic==nil?[NSDictionary dictionary]:subdic forKey:@"StoreData"];
                array = (NSArray *)[subdic objectForKey:@"pic"];
                if (array.count >0) {
                    imagepath = [NSString stringWithFormat:@"%@/local/%@",localpath,[array objectAtIndex:0]];
                }
                
                itemview.dataSource = dic;
                break;
            case 4:

                subdic = [templateDic objectForKey:@"right"];
                title = @"会员权利";
                [dic setObject:title forKey:@"StoreTitle"];
                [dic setObject:subdic==nil?[NSDictionary dictionary]:subdic forKey:@"StoreData"];
                array = (NSArray *)[subdic objectForKey:@"pic"];
                if (array.count >0) {
                    imagepath = [NSString stringWithFormat:@"%@/local/%@",localpath,[array objectAtIndex:0]];
                }
                
                itemview.dataSource = dic;
                break;
            case 5:
                title = @"会员活动";
                array = (NSArray *)[templateDic objectForKey:@"activities"];
                [dic setObject:title forKey:@"StoreTitle"];
                [dic setObject:array==nil?[NSArray array]:array forKey:@"StoreData"];
                
                if (array.count >0) {
                    imagepath = [NSString stringWithFormat:@"%@/local/activities/%@",localpath,[array objectAtIndex:0]];
                }
                itemview.dataSource = dic;
                break;
                
            default:
                break;
        }
        
        [itemview LoadTitle:title ImagePath:imagepath];
    }
}

-(void)ItemBeTouch:(StoreItemView *)itemview
{
    if (itemview.dataSource == nil) {
        return;
    }
    
    int index = itemview.tag-100;
    switch (index) {
        case 0:
        case 1:
        case 2:
        case 3:
        case 4:
            [[NSNotificationCenter defaultCenter]postNotificationName:Notification_Navigate_To_Store object:itemview.dataSource userInfo:nil];
            break;
        case 5:
            [[NSNotificationCenter defaultCenter]postNotificationName:Notification_Navigate_To_Member object:itemview.dataSource userInfo:nil];
            break;
            
        default:
            break;
    }
    
}

-(void)dealloc
{
    [super dealloc];
}

@end
